import request from '@/utils/request'

/**
 * 刷新页面更新商品状态（现价、库存、是否有效）
 * @param {*} skuId
 * @returns
 */
export const updateGoodsAPI = (skuId) => {
  return request(`/goods/stock/${skuId}`, 'get')
}

/**
 * 本地购物车合并（就是把本地的购物车列表和后台的合在一起）
 * 已登录：[{id:1, name: '篮球', count: 1, selected: true }， {id:1, name: '足球', count: 2, selected: true }] -> 在数据库中
 * 未登录：[{id:1, name: '足球', count: 2, selected: true }] -> 在本地
 * @param {*} cartList 对象数组[{skuId,selected,count}, {skuId,selected,count}]
 * @returns
 */
export const mergeLocalCartListAPI = (cartList) => {
  return request('/member/cart/merge', 'post', cartList)
}

/**
 * 获取购物车列表
 * @returns
 */
export const getCartListAPI = () => {
  return request('/member/cart', 'get')
}

/**
 * 加入购物车
 * @param {*} skuId
 * @param {*} count
 * @returns
 */
export const insertCartAPI = (skuId, count) => {
  return request('/member/cart', 'post', {
    skuId,
    count
  })
}

/**
 * 根据ids的数组删除商品
 * @param {*} ids
 * @returns
 */
export const delCartAPI = (ids) => {
  return request('/member/cart', 'delete', { ids })
}

/**
 * 修改购物车信息
 * @param {*} param0
 * @returns
 */
export const updateCartAPI = ({ skuId, selected, count }) => {
  return request(`/member/cart/${skuId}`, 'put', {
    selected,
    count
  })
}

/**
 * 全选或取消全选
 * @param {*} ids
 * @param {*} selected
 * @returns
 */
export const toggleAllCartAPI = (ids, selected) => {
  return request('/member/cart/selected', 'put', {
    ids,
    selected
  })
}

/**
 * 获取确认信息
 */
export const checkoutInfoAPI = () => {
  return request('/member/order/pre', 'get')
}
